package com.myaxuan.service.permissions.impl;


import com.myaxuan.entity.permissions.Menu;
import com.myaxuan.entity.permissions.RoleMenu;
import com.myaxuan.entity.permissions.Tree;
import com.myaxuan.mapper.permissions.MenuMapper;
import com.myaxuan.mapper.permissions.RoleMenuMapper;
import com.myaxuan.service.permissions.MenuService;
import com.myaxuan.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by pc255 on 2018/1/31.
 */
@Service
public class MenuServiceImpl  implements MenuService {

	@Autowired
	private MenuMapper menuMapper;

	@Autowired
	private RoleMenuMapper roleMenuMapper;

	@Autowired
	private RedisUtil redisUtil;




	@Override
	public int deleteByPrimaryKey(String menuId) {
		return menuMapper.deleteByPrimaryKey(menuId);
	}

	@Override
	public int insertSelective(Menu record) {
		return menuMapper.insertSelective(record);
	}

	@Override
	public int updateByPrimaryKeySelective(Menu record) {
		return menuMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<Menu> findAll(Integer page ,Integer limit) {
		return menuMapper.findAll(page,limit);
	}


	@Override
	public List<Menu> selectByPrimaryKey(String menuId) {
		return menuMapper.selectByPrimaryKey(menuId);
	}

	@Override
	public List<Menu>  findMenuByUserId(String id){
		List<Menu> menuList = menuMapper.findMenuByUserId(id);
		List<Menu> menuExtendsList = new ArrayList<Menu>();
		List<Menu> menuChildList =null;
		for(Menu item:menuList){
			if(!item.getLevel().equals("1")){
				continue;
			}else{
				menuChildList = new ArrayList<Menu>();
			}
			for (Menu item2:menuList){
				if(item2.getLevel().equals("2") && item2.getParentId().equals(item.getId()) ){
					menuChildList.add(item2);
				}else{
					continue;
				}
			}
			item.setMenuList(menuChildList);
			menuExtendsList.add(item);
		}
		return menuExtendsList;
	}

	public Boolean  checkoutContianSource(String roleId ,String menuId ){
		List<RoleMenu> roleMenusList = null;
		boolean flag=false;
		if( redisUtil.lRange("roleMenusCache" ,0 ,-1).size() > 0 ){
			roleMenusList=(List)redisUtil.lRange("roleMenusCache" ,0 ,0).get(0);
			for (RoleMenu item:roleMenusList){
				if(item.getRoleId().equals(roleId) && item.getMenuId().equals(menuId)){
					flag = true;
					break;
				}
			}
		}else{
			roleMenusList = roleMenuMapper.selectByPrimaryKey(roleId);
			redisUtil.lPush("roleMenusCache"  ,roleMenusList);
			for (RoleMenu item:roleMenusList){
				if(item.getRoleId().equals(roleId) && item.getMenuId().equals(menuId)){
					flag = true;
					break;
				}
			}
		}


		return flag;
	}


	@Override
	public List<Tree>  roleContianSource(String id){
		List<Menu> menuList=menuMapper.findAll(0,0);
		List<Tree> treeChildrenList = null;
		List<Tree> treeList= new ArrayList<>();
		Tree treeParent = null;
		Tree treeChildren = null;
		//构造节点
		for(Menu item:menuList){
			if(!item.getLevel().equals("1")){
				continue;
			}else{
				treeParent = new Tree();
				treeParent.setId(item.getId());
				treeParent.setText(item.getMenuName());
				treeParent.setChecked(checkoutContianSource(id ,item.getId()));
				treeChildrenList = new ArrayList<>();
			}
			for (Menu item2:menuList){
				if(item2.getLevel().equals("2") && item2.getParentId().equals(item.getId()) ){
					treeChildren = new Tree();
					treeChildren.setId(item2.getId());
					treeChildren.setText(item2.getMenuName());
					treeChildren.setChecked(checkoutContianSource(id ,item.getId()));
					treeChildrenList.add(treeChildren);
				}else{
					continue;
				}
			}
			treeParent.setChildren(treeChildrenList);
			treeList.add(treeParent);
		}
		return treeList;
	}
	@Override
	public Integer count() {
		return menuMapper.count();
	}
}
